﻿
@model Kooboo.CMS.Web.Areas.Contents.Models.SelectableViewModel
@using Kooboo.CMS.Content.Query
@using Kooboo.CMS.Content.Models
@using Kooboo.CMS.Web.Areas.Contents.Controllers
@{
    var schema = (Kooboo.CMS.Content.Models.Schema)ViewData["Schema"];
    var folder = (Kooboo.CMS.Content.Models.TextFolder)ViewData["Folder"];
    var routes = ViewContext.RequestContext.AllRouteValues();
    this.ViewBag.IsEven = true;
    var parentUUID = routes["parentUUID"] ?? "";
    var pagedList = Model.Contents;
    //used in Content_Order
    this.ViewContext.Controller.ViewData["ContentPagedList"] = pagedList;
    
    var parent = folder.Parent;

    var nameList = new List<string>() { folder.FriendlyText };

    while (parent != null)
    {
        parent = Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(parent);
        if (parent != null)
        {
            nameList.Add(parent.FriendlyText);
            parent = parent.Parent;
        }
        
    }

    nameList.Reverse();   
}
<input type="hidden" name="FriendlyFolderName" value="@string.Join("/", nameList)"/>
<div class="command clearfix">
    @Html.Partial("Search")
</div>
<form id="selectedForm" action="">
<div class="table-container clearfix">
    <table class="datasource">
        <thead>
            <tr>
                <th class="checkbox">
                </th>
                		<th class="common title">Title</th>
		<th class="common utccreationdate">Creation date</th>
		<th class="common published">Published</th>

            </tr>
        </thead>
 @if (Model.ChildFolders != null)
        {
            <tbody>
                @foreach (dynamic item in Model.ChildFolders)
                {
                    <tr class="folderTr">
                        <td>                            
                        </td>
                        <td>
                            @if (!string.IsNullOrEmpty(item.SchemaName))
                            {
                                <a class="f-icon folder" href="@this.Url.Action("SelectCategories", ViewContext.RequestContext.AllRouteValues().Merge("FolderName", (object)(item.FullName)).Merge("FullName", (object)(item.FullName)))" >
                                    @Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(item).FriendlyText</a>
                            }
                            else
                            {
                                <a class="f-icon folder" href="@this.Url.Action("SelectCategories", ViewContext.RequestContext.AllRouteValues().Merge("controller", "TextFolder").Merge("FolderName", (object)(item.FullName)).Merge("FullName", (object)(item.FullName)))" >
                                    @Kooboo.CMS.Content.Models.IPersistableExtensions.AsActual(item).FriendlyText</a>
                            }
                        </td>
                        <td colspan="2">
                        </td>                      
                    </tr>
                }
            </tbody>
        }
        <tbody>
           @AddTreeItems(pagedList, schema, folder, true)
        </tbody>
    </table>
    <div class="pager">
        @Html.Pager(pagedList)
    </div>
</div>
</form>
@helper AddTreeItems(IEnumerable<TextContent> items, Schema schema, TextFolder folder, bool isRoot)
    {
        
        if (Repository.Current.EnableWorkflow == true)
        {
            items = Kooboo.CMS.Content.Services.ServiceFactory.WorkflowManager.GetPendWorkflowItemForContents(Repository.Current, items.ToArray(), User.Identity.Name);
        }
        if (items.Count() > 0)
        {
            foreach (dynamic item in items)
            {
            this.ViewBag.IsEven = (!this.ViewBag.IsEven);
    <tr id="@item.UUID" class="@(schema.IsTreeStyle? "parent collapsed":"") @(this.ViewBag.IsEven ? "even " : "")@((!isRoot && !string.IsNullOrEmpty(item.ParentUUID)) ? "child-of-node-" + item.ParentUUID : "")">
        <td>
            @if (Model.SingleChoice)
            {
                <input type="radio" value='@item["UUID"]' name="chkContent"/> 
            }
            else
            {
                <text>@Html.CheckBox("chkContent", false, new { value = item["UUID"], @class = "checkboxList" })</text> 
            }
        </td>
        		<td>
		<span class="expander" style="padding-left: 19px; margin-left: -19px;"></span>@Kooboo.CMS.Form.Html.HtmlCodeHelper.RenderColumnValue(item.Title)</td>
		<td class="date">@(DateTime.Parse(item["UtcCreationDate"].ToString()).ToLocalTime().ToShortDateString())</td>
		<td class="action"><span class="o-icon @((item.Published!=null && item.Published == true)?"tick":"cross")"></span></td>
    </tr>     
            }
        }
}

<table id="treeNode-template" style="display: none" data-model="JsonModel">
    <tbody data-bind="foreach:{data:Model,as:'item'}">  
           
      <tr class="parent collapsed" data-bind="attr:{id:item.UUID,parentChain:item._ParentChain_}">
        <td>
            @if (Model.SingleChoice)
            {
                <input type="radio" name="chkContent" data-bind="attr:{value:item.UUID}"/> 
            }
            else
            {                
               <input type="checkbox" name="chkContent" class = "checkboxList" data-bind="attr:{value:item.UUID}"/>
            }
        </td>
		<td class="treeStyle">
		<span class="expander" style="padding-left: 19px; margin-left: -19px;"></span><span  data-bind="html:item.Title"></span></td>
        <td class="date" data-bind="html:item._LocalCreationDate_"></td>
        <td class="action"><span data-bind="attr: { 'class': item.Published == true?'o-icon tick': 'o-icon cross' }"></span></td>
    </tr>     
    </tbody>
</table>

@if(schema.IsTreeStyle){
@Html.Partial("_TreeDataScripts","readonly")
}
